package common;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author vivi
 * @description
 * @date 2024/5/8
 */
public class FileUtil {
    //负责把filePath对应的文件内容读取出来，放到返回值中
    public static String readFile(String filePath) {
        StringBuilder result = new StringBuilder();
        try(FileReader fileReader = new FileReader(filePath)) {
            while (true){
                int ch = fileReader.read();
                if(ch == -1){
                    break;
                }
                result.append((char)ch);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result.toString();
    }
    //负责把content写入到 filePath 对应的文件中
    public static void writeFile(String filePath, String content) {
        try (FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FileUtil.writeFile("E:\\105\\项目\\JavaOJ\\test3.txt", "hello~~1");
        String content = FileUtil.readFile("E:\\105\\项目\\JavaOJ\\test3.txt");
        System.out.println(content);
    }
}
